package com.joeyzh.utils;

import android.util.Base64;

import com.joey.base.util.LogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Joey on 2019/3/22.
 *
 * @author by Joey
 */

public class StringParseHelper {

    /**
     * {"codeKey":"code","valueKey":"value"}
     * 这种json格式获取value值
     *
     * @param json
     * @param valueKey
     * @param codeKey
     * @return
     */
    public static String parseValue(String json, String valueKey, String codeKey) {
        try {
            if (json == null || codeKey == null) {
                return null;
            }
            String[] temp = parseFirstStep(json, valueKey, codeKey);
            if (temp == null || temp.length < 2) {
                return null;
            }
            String value = parseSecond(temp[1]);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> parseValueArray(String json, String valueKey, String codeKey) {
        List<String> array = new ArrayList<>();
        if (json == null || codeKey == null) {
            return array;
        }
        String tempArray[] = parseFirstStep(json, valueKey, codeKey);
        if (tempArray == null || tempArray.length < 2) {
            return array;
        }
        for (int i = 1; i < tempArray.length; i++) {
            try {
                String value = parseSecond(tempArray[i]);
                array.add(value);
            } catch (Exception e) {

            }
        }
        return array;
    }

    public static List<String> parseValueArrayBase64(String json, String valueKey, String code) {
        List<String> array = new ArrayList<>();
        if (json == null || code == null) {
            return array;
        }
        String tempArray[] = parseFirstStep(json, valueKey, code);
        if (tempArray == null || tempArray.length < 2) {
            return array;
        }
        for (int i = 1; i < tempArray.length; i++) {
            try {
                String value = parseSecond(tempArray[i]);
                array.add(value);
            } catch (Exception e) {

            }
        }
        return array;
    }

    private static String[] parseFirstStep(String json, String valueKey, String code) {
        String split = String.format("\"%s\",\"%s\":\"", code, valueKey);
        return json.split(split);
    }

    private static String parseSecond(String json) throws Exception {
        String[] temp1 = json.split("\"\\}");
        String value = temp1[0];
        return value;
    }


    public static void test() {
        String json = "{\"command\":{\"WorkOrderOperationList\":[{\"TeamId\":\"32b842b9-cae9-4608-b65e-811595508547\",\"StartTime\":\"2019-03-21T19:52:07.085Z\",\"PersonId\":\"123456\",\"PersonName\":\"UserTest\",\"ShiftId\":\"2a122db9-0c65-498d-b968-055178382926\",\"WorkOrderParamList\":[{\"ParamNId\":\"ChassisDamageDesc\",\"ParamValue\":\"地盘损伤描述1\"},{\"ParamNId\":\"ElectrodeDamageDesc\",\"ParamValue\":\"电极1\"},{\"ParamNId\":\"ExhaustGasLeakageDesc\",\"ParamValue\":\"23\"},{\"ParamNId\":\"MaintenanceItems\",\"ParamValue\":\"红色\"},{\"ParamNId\":\"MaintenanceItems\",\"ParamValue\":\"尾气内漏\"},{\"ParamNId\":\"MaintenanceDesc\"},{\"ParamNId\":\"Auto\",\"ParamValue\":\"false\"},{\"ParamNId\":\"Manual\",\"ParamValue\":\"false\"}],\"WorkOrderId\":\"dcee1127-fed6-4f74-baf7-7075b377d5e9\",\"NId\":\"StartClean\",\"EndTime\":\"2019-03-21T19:52:07.083Z\",\"CoverCurrent\":false}],\"WorkOrderId\":\"dcee1127-fed6-4f74-baf7-7075b377d5e9\"}}";
        String value = StringParseHelper.parseValue(json, "ParamValue", "ChassisDamageDesc");
        LogUtils.a("value = " + value);
        List<String> array = StringParseHelper.parseValueArray(json, "ParamValue", "MaintenanceItems");
        for (String item : array) {
            LogUtils.a("item = " + item);
        }
    }

    public static String format(String original) {
        final Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher(original);
        String result = m.replaceAll("");
        return result;
    }


}
